package com.game.doudizhu.service;

import com.game.doudizhu.model.Player;
import com.game.doudizhu.model.ShopItem;
import com.game.doudizhu.model.PlayerItem;
import com.game.doudizhu.repository.ShopItemRepository;
import com.game.doudizhu.repository.PlayerItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopService {
    
    @Autowired
    private ShopItemRepository shopItemRepository;
    
    @Autowired
    private PlayerItemRepository playerItemRepository;
    
    @Autowired
    private PlayerService playerService;
    
    public List<ShopItem> getAvailableItems(Long playerId) {
        // 获取玩家未拥有的商品ID列表
        List<Long> notOwnedItemIds = playerItemRepository.findNotOwnedItemIds(playerId);
        
        // 返回可购买的商品列表
        return shopItemRepository.findByIdInAndAvailableTrue(notOwnedItemIds);
    }
    
    public List<PlayerItem> getPlayerItems(Long playerId) {
        return playerItemRepository.findByPlayerId(playerId);
    }
    
    @Transactional
    public PlayerItem purchaseItem(Long playerId, Long itemId) {
        Player player = playerService.getPlayer(playerId);
        ShopItem item = shopItemRepository.findById(itemId)
            .orElseThrow(() -> new RuntimeException("商品不存在"));
            
        if (!item.getAvailable()) {
            throw new RuntimeException("商品已下架");
        }
        
        if (player.getScore() < item.getPrice()) {
            throw new RuntimeException("积分不足");
        }
        
        // 检查是否已拥有
        if (playerItemRepository.existsByPlayerAndItem(player, item)) {
            throw new RuntimeException("已拥有该商品");
        }
        
        // 扣除积分
        player.setScore(player.getScore() - item.getPrice());
        playerService.updatePlayer(player);
        
        // 添加物品
        PlayerItem playerItem = new PlayerItem();
        playerItem.setPlayer(player);
        playerItem.setItem(item);
        playerItem.setPurchaseTime(LocalDateTime.now());
        
        return playerItemRepository.save(playerItem);
    }
    
    @Transactional
    public PlayerItem toggleEquip(Long playerId, Long itemId) {
        Player player = playerService.getPlayer(playerId);
        PlayerItem item = playerItemRepository.findById(itemId)
            .orElseThrow(() -> new RuntimeException("物品不存在"));
            
        if (!item.getPlayer().getId().equals(playerId)) {
            throw new RuntimeException("不是该物品的所有者");
        }
        
        // 如果物品已装备，则取消装备
        if (item.getEquipped()) {
            item.setEquipped(false);
            return playerItemRepository.save(item);
        }
        
        // 如果物品未装备，则先取消同类型物品的装备状态，再装备当前物品
        playerItemRepository.findByPlayerAndItemType(player, item.getItem().getType())
            .forEach(i -> {
                i.setEquipped(false);
                playerItemRepository.save(i);
            });
            
        item.setEquipped(true);
        return playerItemRepository.save(item);
    }
} 